package lg.minecraft.plugin.pokemonmenu.gui;

import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.PokemonPlayer;
import lg.minecraft.plugin.pokemonmenu.event.EventFactory;
import lg.minecraft.plugin.pokemonmenu.event.ItemToPokemonEvent;
import lg.minecraft.plugin.pokemonmenu.manager.PokemonPlayerManger;
import lg.minecraft.plugin.pokemonmenu.util.PokemonNBTUtil;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 * @author langle__
 * @version 1.0
 */
public class ItemToPokemonInventory extends LGInventory {

    private final PokemonNBTUtil.Entry pokemonEntry;
    private final ItemStack itemStack;
    private final Pokemon pokemon;

    private boolean confirmed = false;

    public ItemToPokemonInventory(PokemonNBTUtil.Entry entry, ItemStack itemStack) {
        super(3, LGPokemonMenu.getMessage().getGuiTitleItemToPokemon());
        this.pokemonEntry = entry;
        this.pokemon = entry.getPokemon();
        this.itemStack = itemStack;
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        inventory.setItem(13, LGPokemonMenu.getGuiItem().getPokemon(pokemon));
        inventory.setItem(11, LGPokemonMenu.getGuiItem().getConfirmButton(LGPokemonMenu.getMainConfig().getToPokemon()));
        inventory.setItem(15, LGPokemonMenu.getGuiItem().getCancelButton());
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();

        if (slot == 11) {
            Economy economy = LGPokemonMenu.getEconomy();
            int value = LGPokemonMenu.getMainConfig().getToPokemon();
            if (!economy.has(player, value)) {
                player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0F, 0.0F);
                return;
            }

            ItemToPokemonEvent itemToPokemonEvent = EventFactory.callItemToPokemonEvent(player, pokemon);

            if (itemToPokemonEvent.isRemoveItem()) confirmed = true;
            if (itemToPokemonEvent.isCancelled()) {
                player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0F, 0.0F);
                if (itemToPokemonEvent.isRemoveItem()) player.closeInventory();
                return;
            }

            economy.withdrawPlayer(player, value);
            confirmed = true;
            pokemonEntry.run();
            PokemonPlayer pokemonPlayer = PokemonPlayerManger.INSTANCE.getPokemonPlayer(player);
            pokemonPlayer.addPartyPokemon(pokemon);
            player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1.0F, 1.8F);
            player.sendMessage(LGPokemonMenu.getMessage().getModelToPokemon());
            player.closeInventory();
        } else if (slot == 15) {
            player.closeInventory();
            player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1.0F, 0.0F);
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
        if (confirmed) return;
        context.getPlayer().getInventory().addItem(itemStack);
    }

}
